package com.cloudcc.mobile.manager;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.text.TextUtils;

import com.cloudcc.cloudframe.CApplication;
import com.cloudcc.cloudframe.model.UserModel;
import com.cloudcc.cloudframe.util.ImageUtils;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.AppConfig;
import com.cloudcc.mobile.util.PreferenceUtils;

import java.io.File;

/**
 * 当前用户管理器
 * 
 * @author wangzhiqiang
 * @date 2015-7-24 下午2:47:02
 * 
 */
public class UserManager {

	private static UserManager instance;
	private Context mContext;
	private UserModel currentUser;
	private final String XML = AppConfig.XML_USER;

	private UserManager(Context context) {
		super();
		this.mContext = context;
	}

	public static UserManager getManager() {
		if (instance == null) {
			instance = new UserManager(CApplication.getApp());
		}
		return instance;
	}

	/**
	 * 保存用户
	 * 
	 * @param user
	 */
	public void saveUser(UserModel user) {
		deleteUser();
		// #1 先保存到内存中
		currentUser = user;
		// #2 同时缓存到文件中
		Editor editor = PreferenceUtils.getEditor(mContext, XML);
		editor.putString(IKey.ID, user.userId)
				.putString(IKey.NAME, user.userName)
				.putString(IKey.MAIL, user.email)
				.putString(IKey.ORG_ID, user.orgId)
				.putString(IKey.ORG_NAME, user.orgName)
				.putString(IKey.JIAOSE, user.roleName)
				.putString(IKey.JIANDAN_GNAME, user.profileName)
				.putString(IKey.APPMAINPAGE, user.appMainPage).commit();
	}

	public void deleteUser() {
		currentUser = null;
		Editor editor = PreferenceUtils.getEditor(mContext, XML);
		editor.remove(IKey.ID).remove(IKey.NAME).remove(IKey.MAIL)
				.remove(IKey.ORG_ID).remove(IKey.ORG_NAME).remove(IKey.JIAOSE)
				.remove(IKey.JIANDAN_GNAME).remove(IKey.APPMAINPAGE).commit();
	}

	/**
	 * 判断用户是否登录
	 * 
	 * @return
	 */
	public boolean isLogin() {

		UserModel user = getUser();
		if (user != null && StringUtils.isNoneBlank(user.userId))
			return true;
		return false;
	}

	/**
	 * 删除所有的用户缓存信息
	 */
	public void clearUser() {
		currentUser = null;
		PreferenceUtils.clear(mContext, XML);
	}

	/**
	 * 获取缓存的用户 调用之前 请先使用has user判断
	 * 
	 * @return 如果不存在就返回null
	 */
	public UserModel getUser() {
		// #1 判断内存中是否保存User 有的话先使用内存中的
		UserModel user = currentUser;
		if (user != null) {
			return user;
		}
		// #2 内存中没有的话 提取文件中的

		user = new UserModel();
		SharedPreferences sharedPreferences = PreferenceUtils.getPreferences(
				mContext, XML);
		user.userId = sharedPreferences.getString(IKey.ID, "");
		user.userName = sharedPreferences.getString(IKey.NAME, "");
		user.email = sharedPreferences.getString(IKey.MAIL, "");
		user.orgId = sharedPreferences.getString(IKey.ORG_ID, "");
		user.orgName = sharedPreferences.getString(IKey.ORG_NAME, "");
		user.roleName = sharedPreferences.getString(IKey.JIAOSE, "");
		user.profileName = sharedPreferences.getString(IKey.JIANDAN_GNAME, "");
		user.profileName = sharedPreferences.getString(IKey.APPMAINPAGE, "");
		if (TextUtils.isEmpty(user.userId)) {
			return user;
		}
		currentUser = user;
		return user;
	}

	/**
	 * 保存用户头像
	 * 
	 * @param logo
	 */
	public void saveLogo(Bitmap logo) {
		// #1 缓存地址
		File file = new File(mContext.getCacheDir(), AppConfig.PATH_CACHE_IMAGE
				+ UserManager.getManager().getUser().userId + "logo.png");
		boolean saveBitmap = ImageUtils.saveBitmap(file.getAbsolutePath(),
				logo, 80);
	}

	/**
	 * 获得头像
	 * 
	 * @return
	 */
	public Bitmap getLogo() {
		File file = new File(mContext.getCacheDir(), AppConfig.PATH_CACHE_IMAGE
				+ UserManager.getManager().getUser().userId + "logo.png");
		return ImageUtils.getBitmap(file.getAbsolutePath());
	}
	/**
	 * 获得头像
	 *
	 * @return
	 */
	public Bitmap getLogoId(String id) {
		File file = new File(mContext.getCacheDir(), AppConfig.PATH_CACHE_IMAGE
				+id + "logo.png");
		return ImageUtils.getBitmap(file.getAbsolutePath());
	}

	/**
	 * 是否有缓存的用户
	 * 
	 * @return
	 */
	public boolean hasUser() {
		UserModel user = getUser();
		if (user == null || TextUtils.isEmpty(user.userId)
				|| TextUtils.isEmpty(user.userName)) {
			clearUser();
			return false;
		}
		return true;
	}



	/**
	 * 是否是注销登录
	 *
	 * @return 如果没有这个字段就i返回 true 默认是 第一次进入系统
	 */
	public boolean zhuxiao() {
		return PreferenceUtils.getBoolean(mContext, XML, IKey.ZHUXIAO, false);
	}


	/**
	 * 保存是不是注销进入系统
	 *
	 * @param frist
	 */
	public void savezhuxiao(boolean frist) {
		PreferenceUtils.commitBoolean(mContext, XML, IKey.ZHUXIAO, frist);
	}

	/**
	 * 是否是第一次进入系统
	 *
	 * @return 如果没有这个字段就i返回 true 默认是 第一次进入系统
	 */
	public boolean isFrist() {
		return PreferenceUtils.getBoolean(mContext, XML, IKey.ISFRIST, true);
	}

	/**
	 * 保存是不是第一次进入系统
	 *
	 * @param frist
	 */
	public void saveFrist(boolean frist) {
		PreferenceUtils.commitBoolean(mContext, XML, IKey.ISFRIST, frist);
	}
	/**
	 * 保存 小网页的 服务器地址
	 * 
	 * @param url
	 */
	public void saveWap(String url) {
		PreferenceUtils.commitString(mContext, XML, IKey.WAP_URL, url);
	}

	/**
	 * 获得小网页的服务器地址
	 * 
	 * @return
	 */
	public String getWap() {
		return PreferenceUtils.getString(mContext, XML, IKey.WAP_URL, "");
	}

	public interface IKey {
		String ID = "user.Id";
		String NAME = "user.name";
		String PWD = "user.password";
		String MAIL = "user.mail";
		String ORG_ID = "user.orgId";
		String ORG_NAME = "user.orgName";
		String JIAOSE = "user.jiaose";
		String ZUZHI_NAME = "user.zuzhiname";
		String JIANDAN_GNAME = "user.jiandangname";
		String APPMAINPAGE = "user.appMainPage";

		/**
		 * 服务器地址
		 */
		String SERVER_URL = "server.url";

		/**
		 * 是否第一次进入系统
		 */
		String ISFRIST = "isFrist";
		String ISFRIST_X = "isFrist_x";
		/**
		 * 小网页 地址
		 */
		String WAP_URL = "wapurl";

		String ZHUXIAO = "zhuxiao";

		/**
		 * 登录的用户名
		 */
		String LOGIN_NAME = "login.name";

	}


}
